package datesource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/14.
 */
//创建数据源，只要创建一次
public class C3p0Utils {
    public static ComboPooledDataSource comboPooledDataSource=null;

   static {
    //c3p0数据源对象
    comboPooledDataSource=new ComboPooledDataSource();

    //设置与数据库的关联
    try {
        comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3307/user");
        comboPooledDataSource.setUser("root");
        comboPooledDataSource.setPassword("123456");

        //设置默认连接数量
        comboPooledDataSource.setInitialPoolSize(5);
        comboPooledDataSource.setMaxPoolSize(15);
        } catch (PropertyVetoException e) {
        e.printStackTrace();
    }

   }
   public static Connection getConnection() throws SQLException {   //数据源方  使用时返回连接
       return comboPooledDataSource.getConnection();
   }

}
